/**
 * Created by xinhuang on 15/6/9.
 */
(function (angular) {
  'use strict';

  angular.module('controllers.customActivity.realtime')
    .controller('realtimePreviewController', realtimePreviewController);

  function realtimePreviewController($scope, $state, provinceAutoComplete, realtimeServer, dialogFormService, activityManager, previewEditor, activityList, previewEdited) {
    $scope.activityManager = activityManager;

    var provinces;

    if(activityManager.configure.provinceCode.length === 31){
      $scope.selectedProvince = '全网31省';
    } else {
      provinces = provinceAutoComplete.getSelectedProvince(activityManager.configure.provinceCode);
      provinces = provinces.map(function(province){
        return province.provinceName;
      });
      $scope.selectedProvince = provinces.join(',');
    }

    realtimeServer.setActivityId(0);

    $scope.save = function () {
      activityManager.configure.layout = previewEditor.layoutGenerator();
      previewEdited.state = true;
      $state.go('sas.realTimeActivity');
    };

    $scope.cancel = function () {
      dialogFormService.confirm('您确定要关闭预览页面？关闭后数据将不保存')
        .then(function(){
          $state.go('sas.realTimeActivity');
        });
    };

    $scope.$on('$destroy', function(){
      dialogFormService.newDialog(undefined, 'preview').then(
        function () {
          activityList.getList();
        },
        function () {
          activityManager.reset();
        });
    })
  }

})(angular);